/**
 * 
 */
package com.demo2do.fms.scheduler.dao.fms;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Save;
import com.demo2do.core.persistence.annotation.Update;
import com.demo2do.core.persistence.annotation.Variable;
import com.demo2do.fms.entity.business.Shipment;
import com.demo2do.fms.scheduler.entity.Branch;
import com.demo2do.fms.scheduler.entity.Vendor;

/**
 * @author Ysy
 *
 */
@Dao
public interface FmsDao {

    /**
     * 
     * @param id
     * @return
     */
    @Query("SELECT id, name, code, version, is_disabled as state FROM VW_BRANCH WHERE id = :id")
    public Branch getBranch(@Variable("id") String id);
    
    /**
     * 
     * @param branch
     */
    @Save("INSERT INTO VW_BRANCH(id, name, code, version, is_disabled) VALUES(:id, :name, :code, :version, :state)")
    public void save(Branch branch);
    
    /**
     * 
     * @param branch
     */
    @Update("UPDATE VW_BRANCH SET name =:name, code = :code, version = :version, is_disabled = :state WHERE id = :id")
    public void edit(Branch branch);
    
    /**
     * 
     * @param id
     * @return
     */
    @Query("SELECT id, version FROM FMS_VENDOR WHERE id = :id")
    public Vendor getVendor(@Variable("id") String id);
    
    /**
     * 
     * @param vendor
     */
    @Save("INSERT INTO FMS_VENDOR " 
                + "(id, code, name, short_name, status, deposit, account_playable_days, scope, bank_name, bank_account, tax_number, legal_person, address, zip_code, mobile, fax, main_contactor, second_contactor, "
                + " main_phone, second_phone, main_email, second_email, remark, branch_id, version, attachment, create_time, last_modify_time, creator, last_operator, is_disabled ) "
        + "VALUES "
                + "(:id, :code, :name, :shortName, :status, :deposit, :accountPlayableDays, :scope, :bankName, :bankAccount, :taxNumber, :legalPerson, :address, :zipCode, :mobile, :fax, :mainContactor, :secondContactor, "
                + " :mainPhone, :secondPhone, :mainEmail, :secondEmail, :remark, :branchId, :version, :attachment, :createTime, :lastModifyTime, :creator, :lastOperator, :disabled ) ")
    public void save(Vendor vendor);
    
    /**
     * 
     * @param vendor
     */
    @Update("UPDATE FMS_VENDOR SET" 
                  + " code = :code, name = :name, short_name = :shortName, status = :status, deposit = :deposit, account_playable_days = :accountPlayableDays, scope = :scope, bank_name = :bankName, bank_account = :bankAccount, "
                  + " tax_number = :taxNumber, legal_person = :legalPerson, address = :address, zip_code = :zipCode, mobile = :mobile, fax = :fax, main_contactor = :mainContactor, second_contactor = :secondContactor, main_phone = :mainPhone, "
                  + " second_phone = :secondPhone, main_email = :mainEmail, second_email = :secondEmail, remark = :remark, branch_id = :branchId, version = :version, attachment = :attachment, " 
                  + " last_operator = :lastOperator, last_modify_time = :lastModifyTime, is_disabled = :disabled WHERE id = :id")
    public void edit(Vendor vendor);
    
    /**
     * @param beginDate
     * @return
     */
    @Query("SELECT MIN(id) AS min, MAX(id) AS max FROM TBL_ALL_SHIPMENT WHERE shipment_date > :beginDate AND is_disabled = 0 AND is_electronic_confirm = 0")
    public Map<String, BigDecimal> loadUnConfirmSection(@Variable("beginDate") Date beginDate);
    
    /**
     * 查询未回单确认调度指令
     * 
     * @param startId
     * @param endId
     * @return
     */
    @Query("SELECT id, load_number, shipment_id FROM TBL_ALL_SHIPMENT WHERE id BETWEEN :startId AND :endId AND is_disabled = 0 AND is_electronic_confirm = 0")
    public List<Shipment> listUnElectronicConfirm(@Variable("startId") Long startId, @Variable("endId") Long endId);
    
    /**
     * 同步电子回单状态
     * 
     * @param ids
     */
    @Update("UPDATE TBL_ALL_SHIPMENT SET is_electronic_confirm = 1 WHERE id IN (:ids)")
    public void syncShipment(@Variable("ids") List<Long> ids);
    
}
